home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 March / EnigmA AMIGA RUN 05 (1996)(G.R. Edizioni)(IT)[!][issue 1996-03][Skylink CD IV].iso / earcd / program / ixemlsrc.lha / ixemul / library / __load_seg.c < prev    next >
C/C++ Source or Header  |  1995-12-23  |  10KB  |  385 lines

  1. /*
  2.  *  This file is part of ixemul.library for the Amiga.
  3.  *  Copyright (C) 1991, 1992  Markus M. Wild
  4.  *
  5.  *  This library is free software; you can redistribute it and/or
  6.  *  modify it under the terms of the GNU Library General Public
  7.  *  License as published by the Free Software Foundation; either
  8.  *  version 2 of the License, or (at your option) any later version.
  9.  *
  10.  *  This library is distributed in the hope that it will be useful,
  11.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  *  Library General Public License for more details.
  14.  *
  15.  *  You should have received a copy of the GNU Library General Public
  16.  *  License along with this library; if not, write to the Free
  17.  *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  *
  19.  *  __load_seg.c,v 1.1.1.1 1994/04/04 04:30:54 amiga Exp
  20.  *
  21.  *  __load_seg.c,v
  22.  * Revision 1.1.1.1  1994/04/04  04:30:54  amiga
  23.  * Initial CVS check in.
  24.  *
  25.  *  Revision 1.5  1992/09/14  01:38:35  mwild
  26.  *  fix a bug with #! expansion (forgot separating /)
  27.  *
  28.  *  Revision 1.4  1992/08/09  20:37:07  amiga
  29.  *  change to use 2.x header files by default
  30.  *
  31.  *  Revision 1.3  1992/07/04  19:02:39  mwild
  32.  *  fix typo, the buffer for interpreter-expansion was ways too small...
  33.  *
  34.  * Revision 1.2  1992/05/22  01:45:00  mwild
  35.  * rewrote interpreter expansion, `should' now work as expected
  36.  *
  37.  * Revision 1.1  1992/05/14  19:55:40  mwild
  38.  * Initial revision
  39.  *
  40.  */
  41.  
  42. #define KERNEL
  43. #include "ixemul.h"
  44. #include "kprintf.h"
  45.  
  46. #include <ctype.h>
  47. #include <string.h>
  48.  
  49. /* 2.0 support */
  50. #include <utility/tagitem.h>
  51. #include <dos/dostags.h>
  52.  
  53. extern void *kmalloc (size_t size);
  54.  
  55. struct my_seg {
  56.   BPTR    segment;    /* the thing our clients can use */
  57.   enum { LOADSEG, RESSEG } type;
  58.   u_int    priv;        /* information depending on type */
  59. };
  60.  
  61.  
  62. static struct my_seg *try_load_seg (BPTR lock, char *name, char **args);
  63.  
  64.  
  65. static inline struct my_seg *
  66. check_resident (char *tmp)
  67. {
  68.   struct my_seg *res = 0;
  69.   struct Segment *seg = 0;
  70.  
  71.       /* big problem: Commo only stores the bare names in the resident
  72.          list. So we have to truncate to the filename part, and so lose
  73.          the ability to explicitly load the disk version even if a 
  74.          resident version is installed */
  75.  
  76.       char *cp = rindex (tmp, '/');
  77.       if (cp)
  78.     tmp = cp + 1;
  79.       else if ((cp = index (tmp, ':')))
  80.     tmp = cp + 1;
  81.  
  82.       Forbid ();
  83.       seg = FindSegment (tmp, 0, 0);
  84.       if (seg)
  85.         {
  86.           /* strange they didn't provide a function for this... */
  87.           if (seg->seg_UC >= 0) 
  88.         seg->seg_UC++;
  89.     }
  90.       Permit ();
  91.  
  92.   if (seg && (res = (struct my_seg *) kmalloc (sizeof (*res))))
  93.     {
  94.       res->segment = seg->seg_Seg;
  95.       res->type    = RESSEG;
  96.       res->priv    = (u_int) seg;
  97.     }
  98.   else if (seg)
  99.     {
  100.       Forbid ();
  101.       if (seg->seg_UC > 0)
  102.     seg->seg_UC--;
  103.       Permit ();
  104.     }
  105.  
  106.   return res;
  107. }
  108.  
  109.  
  110. static inline struct my_seg *
  111. check_loadseg (char *tmp)
  112. {
  113.   struct my_seg *res = 0;
  114.   BPTR seg;
  115.   
  116.   seg = LoadSeg (tmp);
  117.   if (seg && (res = kmalloc (sizeof (*res))))
  118.     {
  119.       res->segment = seg;
  120.       res->type       = LOADSEG;
  121.       res->priv    = seg;
  122.     }
  123.   else if (seg)
  124.     UnLoadSeg (seg);
  125.     
  126.   return res;
  127. }
  128.  
  129.  
  130. void
  131. __free_seg (BPTR *seg)
  132. {
  133.   struct my_seg *ms;
  134.   
  135.   ms = (struct my_seg *) seg;
  136.   
  137.   if (ms->type == RESSEG)
  138.     {
  139.       struct Segment *s = (struct Segment *) ms->priv;
  140.  
  141.       Forbid ();
  142.       if (s->seg_UC > 0)
  143.     s->seg_UC--;
  144.       Permit ();
  145.     }
  146.   else
  147.     UnLoadSeg (ms->priv);
  148.  
  149.   kfree (ms);
  150. }
  151.  
  152.  
  153. /*
  154.  * This function does what LoadSeg() does, and a little bit more ;-)
  155.  * Besides walking the PATH of the user, we try to do interpreter expansion as
  156.  * well. But, well, we do it a little bit different then a usual Amiga-shell.
  157.  * We check the magic cookies `#!' and `;!', and if found, run the interpreter
  158.  * specified on this first line of text. This does *not* depend on any script
  159.  * bit set!
  160.  * If this check is negative, the script bit is tested. If set, the special
  161.  * BPTR (-2) is returned, this is the hint to execve() to go and invoke
  162.  * system() with the command line. In this case *NO* interpreter expansion
  163.  * takes place, as the expansion must have already failed before.
  164.  */
  165.  
  166. /*
  167.  * IMPORTANT: only call this function with all signals masked!!! 
  168.  */
  169.  
  170. /*
  171.  * name:        the name of the command to load. Can be relative to installed PATH
  172.  * args:        if set, a string to the first part of an expanded command is stored
  173.  */
  174.  
  175. BPTR *
  176. __load_seg (char *name, char **args)
  177. {
  178.   BPTR lock;
  179.   struct my_seg *seg;
  180.   
  181.   /* perhaps the name is vanilla enough, so that even LoadSeg() groks it? */
  182.   if (args) *args = 0;
  183.  
  184.   seg = check_resident (name);
  185.  
  186.   if (! seg)
  187.     seg = check_loadseg (name);
  188.  
  189.   if (seg)
  190.     return &seg->segment;
  191.  
  192.   /* try to lock the file (using __lock() provides full path-parsing ;-)) */
  193.  
  194.   lock = __lock (name, ACCESS_READ);
  195.   if (lock)
  196.     {
  197.       /* this is tricky.. it is legal to CurrentDir() to a file. This is what
  198.        * we do here, we try to LoadSeg("") afterwards ;-)) */
  199.       seg = try_load_seg (lock, "", args);
  200.  
  201.       __unlock (lock);
  202.     }
  203.  
  204.   /* now we may have a valid segment */
  205.   if (seg)
  206.     return &seg->segment;
  207.  
  208.   /* if the command was specified with some kind of path, for example with a
  209.    * device or a directory in it, we don't run it thru the PATH expander
  210.    */
  211.   if (strpbrk (name, ":/"))
  212.     return 0;
  213.  
  214.   /* so the command is not directly addressable, but perhaps it's in our PATH? */
  215.   {
  216.     struct Process *me = (struct Process *)((*(struct ExecBase **)4)->ThisTask);
  217.     struct CommandLineInterface *cli;
  218.  
  219.     /* but we need a valid CLI then */
  220.     if ((cli = BTOCPTR (me->pr_CLI)))
  221.       {
  222.     struct path_element {
  223.       BPTR    next;
  224.       BPTR     lock;
  225.     } *lock_list;
  226.  
  227.     for (lock_list = BTOCPTR (cli->cli_CommandDir);
  228.          lock_list;
  229.          lock_list = BTOCPTR (lock_list->next))
  230.       {
  231. #ifdef 0
  232.             KPRINTF(("__load_seg: trying PATH component: next = $%lx, lock = $%lx\n",
  233.                 lock_list->next, lock_list->lock));
  234. #endif
  235.  
  236.         if ((seg = try_load_seg (lock_list->lock, name, args)))
  237.           break;
  238.       }
  239.       }
  240.   }
  241.   
  242.   if (seg)
  243.     return &seg->segment;
  244.  
  245.   errno = ENOENT;
  246.   KPRINTF (("&errno = %lx, errno = %ld\n", &errno, errno));
  247.   return 0;
  248. }
  249.  
  250. static struct my_seg *
  251. try_load_seg (BPTR lock, char *name, char **args)
  252. {
  253.   BPTR ocd;
  254.   struct my_seg *seg;
  255.  
  256.   if (args) *args = 0;
  257.   
  258.   ocd = CurrentDir (lock);
  259.   
  260.   seg = check_loadseg (name);
  261.  
  262.   /* try to do interpreter - expansion, but only if args is non-zero */
  263.   if (! seg && args)
  264.     {
  265.       int fd;
  266.       char magic[5];
  267.       struct stat stb;
  268.       
  269.       if (syscall (SYS_stat, name, &stb) == 0 && S_ISREG (stb.st_mode))
  270.     {
  271.       if ((fd = syscall (SYS_open, name, 0)) >= 0)
  272.             {
  273.               /*
  274.                *  If the .key line of an AmigaDOS script isn't the first
  275.                *  line of the script, the AmigaDOS shell gets very confused.
  276.                *  Therefore, we skip the first line if it begins with .key,
  277.                *  and we test the second line for #! or ;!.
  278.                */
  279.           if (syscall (SYS_read, fd, magic, 4) == 4)
  280.             {
  281.               magic[4] = 0;
  282.               if (!strcasecmp(magic, ".key"))
  283.                 {
  284.                   /* skip this line */
  285.                   while (syscall (SYS_read, fd, magic, 1) == 1)
  286.                     if (magic[0] == '\n')
  287.                       {
  288.                         syscall (SYS_read, fd, magic, 4);
  289.                         break;
  290.                       }
  291.                 }
  292.             }
  293.           if ((magic[0] == '#' || magic[0] == ';') && magic[1] == '!')
  294.             {
  295.               char interp[MAXPATHLEN + 1], *interp_start;
  296.               int n;
  297.           
  298.               interp[0] = magic[2];
  299.               interp[1] = magic[3];
  300.               if ((n = syscall (SYS_read, fd, interp + 2, MAXINTERP - 2)) > 0)
  301.             {
  302.               char *cp, ch;
  303.               char *interp_path;
  304.  
  305.               /* okay.. got one.. terminate with 0 and try to find end of it */
  306.               interp[n] = 0;
  307.               for (interp_start = interp; isspace(*interp_start) && interp_start < interp + n; interp_start++);
  308.               for (cp = interp_start; cp < interp + n; cp++)
  309.                 if (*cp == 0 || isspace (*cp)) break;
  310.               ch = *cp;
  311.               *cp = 0;
  312.  
  313.               /* okay, lets try to load this instead. Call us recursively,
  314.                * but leave out the argument-argument, so we can't get
  315.                * into infinite recursion. Interpreter-Expansion is only
  316.                * done the first time __load_seg() is called from
  317.                * execve()
  318.                */
  319.               seg = (struct my_seg *) __load_seg (interp_start, 0);
  320.               *cp = ch;
  321.               if (! seg)
  322.                 goto ret;
  323.           
  324.               /* in this case, set the argument as well. 
  325.                */
  326.  
  327.               /* first skip intergap whitespace */
  328.               for (;cp < interp + n; cp++)
  329.             if (!*cp || ! isspace (*cp) || *cp == '\n')
  330.               break;
  331.  
  332.               if (*cp && *cp != '\n')
  333.                 {
  334.               /* we read a certain amount of bytes, but we 
  335.                * unconditionally stop when we hit newline
  336.                */
  337.               interp_path = cp;
  338.  
  339.                   /* crop any further arguments, only ONE argument
  340.                    * is supported
  341.                */
  342.                   for (;cp < interp + n; cp++) 
  343.                 if (isspace (*cp)) 
  344.                   break;
  345.                   if (cp < interp + n)
  346.                 *cp = 0;
  347.  
  348.               *cp++ = ' ';
  349.             }
  350.               else
  351.             cp = interp_path = interp_start;
  352.  
  353.               if (name[0] != '/' && !index (name, ':'))
  354.             {
  355.               if (NameFromLock (lock, cp, 
  356.                         MAXPATHLEN-(cp-interp)) == -1)
  357.                             {
  358.                               if (*name)
  359.                     strcat (strcat (cp, "/"), name);
  360.                 }
  361.               else
  362.                 strcpy (cp, name);
  363.             }
  364.               else
  365.             strcpy (cp, name);
  366.  
  367.               *args = (char *) syscall (SYS_strdup, interp_path);
  368.             }
  369.             }
  370.           syscall (SYS_close, fd);
  371.         }
  372.  
  373. ret:  
  374.       /* check to see if script bit is set, no matter whether we could
  375.        * actually open the file or not. If set, set seg to magic BPTR */
  376.       if (! seg && (stb.st_amode & FIBF_SCRIPT))
  377.         seg = (struct my_seg *) -2;
  378.         }
  379.     }
  380.  
  381.   CurrentDir (ocd);
  382.   
  383.   return seg;
  384. }
  385.